//
//  MemberInfoController.m
//  yameishe3
//
//  Created by pro on 2017/3/3.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "MemberInfoController.h"
#import "MemberEditInfoView.h"
#import "MemberChangeEmailController.h"
#import "MemberChangePhoneController.h"



#import "YMSMemberManager.h"
@interface MemberInfoController ()
{
   __weak IBOutlet  UIImageView* _photoView;
    UIImage* _photofile;
    
   __weak IBOutlet  UIScrollView *_scrollView;
    MemberEditInfoView *_memberView;
}


@end

@implementation MemberInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self controllerSetting];
    self.navigationController.navigationBar.translucent = YES;

    _memberView = [[[NSBundle mainBundle]loadNibNamed:@"MemberEditInfoView" owner:self options:nil] firstObject];
//    _memberView.backgroundColor = [UIColor blueColor];
    [_scrollView addSubview:_memberView];
    
    [_memberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(@(480));
    }];
    _scrollView.contentSize = CGSizeMake(0, 480);

    [self setUpMemberInfo];
    [_photoView memberImageView];

    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self getMemberInfo];
}
-(void)setUpMemberInfo
{
    [_memberView.setImgBtn addTarget:self action:@selector(selectPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [_memberView.changeEmailBtn addTarget:self action:@selector(changeEmail) forControlEvents:UIControlEventTouchUpInside];
    [_memberView.changePhoneBtn addTarget:self action:@selector(changePhoneNum) forControlEvents:UIControlEventTouchUpInside];
    [_memberView.confirmBtn addTarget:self action:@selector(confirmUpdateInfo) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)getMemberInfo
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    YMSMember *member = [YMSMember loginMember];
    [dic setValue:member.token forKey:@"_token"];
    [[YMSMemberManager alloc]MemberBasicInfoWithDic:dic successBlock:^(NSDictionary *result) {
        
        [self setInfoData:result];
    } failureBlock:^(NSString *error) {
        
    }];
}
-(void)setInfoData:(NSDictionary*)dic
{
    YMSMember *member = [YMSMember loginMember];
    [member memberInitWithDic:dic];
    [YMSMember setLoginMember:member];
    [YMSMember saveLoginMember];
    _memberView.member = [YMSMember loginMember];
    
}
-(void)viewWillDisappear:(BOOL)animated {
    // 反注册键盘
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)selectPhoto:(id)sender
{
        [[[DFImagePickerController alloc] init] showInViewController:self completion:^(UIImage *image, UIImage *srcImage) {
            //          _photofile=[[UIImage alloc]init];
            _photofile=image;
            _photoView.image=image;
//            _changePhoto=YES;
        } cancel:nil noAvailable:nil];

}

-(void)changePhoneNum
{
    MemberChangePhoneController *vc = [[UIStoryboard storyboardWithName:@"Member" bundle:nil] instantiateViewControllerWithIdentifier:@"MemberChangePhoneController"];
    [self.navigationController pushViewController:vc animated:YES];
    

}

-(void)changeEmail
{
    MemberChangeEmailController *vc = [[UIStoryboard storyboardWithName:@"Member" bundle:nil] instantiateViewControllerWithIdentifier:@"MemberChangeEmailController"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)confirmUpdateInfo
{
    NSMutableDictionary *dic = [self getSubmitDic];
    
   [[YMSMemberManager sharedInstance] MemberUpdateInfoWithDic:dic successBlock:^(NSDictionary *result) {
       [self getMemberInfo];
   } failureBlock:^(NSString *error) {
    
   }];
}

-(NSMutableDictionary*)getSubmitDic
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[YMSMember loginMember].token forKey:@"_token"];
    [dic setValue:_memberView.companyNameTF.text forKey:@"company_name"];
    [dic setValue:_memberView.linkerTF.text forKey:@"contact_person"];
    [dic setValue:_memberView.nickNameTF.text forKey:@"nickname"];
    [dic setValue:_memberView.QQTF.text forKey:@"user_QQ"];
    [dic setValue:@"update_info" forKey:@"type"];
    return dic;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    for (UIView *view in _memberView.subviews) {
        for (UIView * subView in view.subviews) {
            [subView resignFirstResponder];
        }
        [view resignFirstResponder];
    }
}


@end
